package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoodController {

    @Autowired
    private GoodService goodService;

    //http://api.leyou.com/api/item/spu/page?key=&saleable=true&page=1&rows=5

    /**
     * 根据条件搜索分页查询商品信息
     * @param key
     * @param saleable
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuBoByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows){


        PageResult<SpuBo> result = this.goodService.querySpuBoByPage(key,saleable,page,rows);

        if(result == null || CollectionUtils.isEmpty(result.getItems())){

            return ResponseEntity.notFound().build();

        }

        return ResponseEntity.ok(result);
    }

    //api.leyou.com/api/item/goods

    /**
     * 新增产品querySpuDetailBySpuId
     * @param spuBo
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> insertGoods(@RequestBody SpuBo spuBo){
        this.goodService.insertGoods(spuBo);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //api.leyou.com/api/item/spu/detail/2

    /**
     * 根据spuId查询spuDetail
     * @param spuId
     * @return
     */
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){

        SpuDetail spuDeatil =  this.goodService.querySpuDetailBySpuId(spuId);

        if(spuDeatil == null){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(spuDeatil);
    }

    //api.leyou.com/api/item/sku/list?id=2

    /**
     * 根据spuId查询Sku数据
     * @param spuId
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId){

        List<Sku> skus = this.goodService.querySkuBySpuId(spuId);

        if(skus == null || CollectionUtils.isEmpty(skus)){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(skus);
    }

    //http://api.leyou.com/api/item/goods

    /**
     * 修改商品
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity updateGoods(@RequestBody SpuBo spuBo){
        this.goodService.updateGoods(spuBo);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //http://api.leyou.com/api/item/spu/delete/32

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("spu/delete/{id}")
    public ResponseEntity deleteGoods(@PathVariable("id") Long id){
        this.goodService.deleteGoods(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //http://api.leyou.com/api/item/spu/upgoods/2

    /**
     * 下架商品
     * @param id
     * @return
     */
    @GetMapping("spu/upgoods/{id}")
    public ResponseEntity upGoods(@PathVariable("id") Long id){
        this.goodService.upGoods(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spuId查询spu数据
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        Spu spu = this.goodService.querySpuById(id);
        if(spu == null ){
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(spu);
    }

    @GetMapping("{id}")
    public ResponseEntity<Spu> getSpuById(@PathVariable("id") Long id){
        Spu spu = goodService.querySpuById(id);
        if (spu == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spu);
    }

    @GetMapping("sku/{skuId}")
    public ResponseEntity<Sku> getSkuBySkuId(@PathVariable("skuId") Long skuId){
        Sku sku = goodService.getSkuBySkuId(skuId);
        if (sku == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(sku);
    }
}
